<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>照片风格转换工具</title>
    <style>
        /* 全局样式 */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
        }

        body {
            background-color: #f5f5f5;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #2c3e50;
        }

        /* 上传区域样式 */
        .upload-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 30px;
            padding: 20px;
            border: 2px dashed #ccc;
            border-radius: 8px;
            background-color: #f9f9f9;
            transition: all 0.3s;
        }

        .upload-section:hover {
            border-color: #3498db;
            background-color: #f0f8ff;
        }

        .upload-btn {
            display: inline-block;
            padding: 12px 24px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
            margin-top: 15px;
        }

        .upload-btn:hover {
            background-color: #2980b9;
        }

        /* 风格选择样式 */
        .style-selector {
            margin: 20px 0;
            text-align: center;
        }

        select {
            padding: 10px 15px;
            border-radius: 5px;
            border: 1px solid #ddd;
            font-size: 16px;
            width: 200px;
            background-color: white;
        }

        /* 图片展示区域 */
        .image-container {
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;
            gap: 20px;
            margin-top: 30px;
        }

        .image-box {
            flex: 1;
            min-width: 300px;
            text-align: center;
            padding: 15px;
            background-color: #f9f9f9;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
        }

        .image-box h3 {
            margin-bottom: 15px;
            color: #2c3e50;
        }

        .image-preview {
            max-width: 100%;
            max-height: 400px;
            border-radius: 5px;
            object-fit: contain;
            border: 1px solid #eee;
        }

        /* 处理按钮 */
        .process-btn {
            display: block;
            margin: 20px auto;
            padding: 12px 30px;
            background-color: #2ecc71;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        .process-btn:hover {
            background-color: #27ae60;
        }

        .process-btn:disabled {
            background-color: #95a5a6;
            cursor: not-allowed;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .image-container {
                flex-direction: column;
            }
        }

        /* 加载动画 */
        .loader {
            display: none;
            border: 5px solid #f3f3f3;
            border-top: 5px solid #3498db;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite;
            margin: 20px auto;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="container">
    <h1>照片风格转换工具</h1>

    <!-- 上传区域 -->
    <div class="upload-section">
        <h2>上传照片</h2>
        <input type="file" id="fileInput" accept="image/*" style="display: none;">
        <button class="upload-btn" id="uploadBtn">选择照片</button>
        <p id="fileName" style="margin-top: 10px; color: #7f8c8d;"></p>
    </div>

    <!-- 风格选择 -->
    <div class="style-selector">
        <label for="styleSelect">选择转换风格：</label>
        <select id="styleSelect">
            <option value="anime">日漫风</option>
            <option value="3d">3D特效</option>
            <option value="handdrawn">手绘风</option>
            <option value="sketch">铅笔画</option>
        </select>
    </div>

    <!-- 处理按钮 -->
    <button class="process-btn" id="processBtn" disabled>开始处理</button>

    <!-- 加载动画 -->
    <div class="loader" id="loader"></div>

    <!-- 图片展示区域 -->
    <div class="image-container">
        <div class="image-box">
            <h3>原始照片</h3>
            <img id="originalImage" class="image-preview" src="" alt="原始照片">
        </div>
        <div class="image-box">
            <h3>处理后照片</h3>
            <img id="processedImage" class="image-preview" src="" alt="处理后照片">
        </div>
    </div>
</div>

<script>
        // DOM元素
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        const fileName = document.getElementById('fileName');
        const styleSelect = document.getElementById('styleSelect');
        const processBtn = document.getElementById('processBtn');
        const originalImage = document.getElementById('originalImage');
        const processedImage = document.getElementById('processedImage');
        const loader = document.getElementById('loader');

        // 上传按钮点击事件
        uploadBtn.addEventListener('click', () => {
            fileInput.click();
        });

        // 文件选择变化事件
        fileInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                // 显示文件名
                fileName.textContent = `已选择: ${file.name}`;

                // 预览图片
                const reader = new FileReader();
                reader.onload = (event) => {
                    originalImage.src = event.target.result;
                    processBtn.disabled = false; // 启用处理按钮
                };
                reader.readAsDataURL(file);
            }
        });

        // 处理按钮点击事件
        processBtn.addEventListener('click', async () => {
            const file = fileInput.files[0];
            if (!file) return;

            // 显示加载动画
            loader.style.display = 'block';
            processBtn.disabled = true;

            try {
                // 创建FormData对象
                const formData = new FormData();
                // 添加文件参数（后端接收参数名为file）
                formData.append('file', file);
                // 添加风格参数（后端接收参数名为style）
                formData.append('style', styleSelect.value);

                // 发送请求到后端
                // 注意：这里需要替换为实际的后端API地址
                const response = await fetch('http://localhost:9090/face/generateHumanAnimeStyle', {
                    method: 'POST',
                    body: formData
                });

                const result = await response.text();

                // 处理结果
                if (result === 'fail') {
                    alert('图片处理失败，请重试或更换图片');
                } else {
                    // 假设返回的是图片URL
                    processedImage.src = result;
                }
            } catch (error) {
                console.error('请求失败:', error);
                alert('请求失败，请检查网络连接');
            } finally {
                // 隐藏加载动画
                loader.style.display = 'none';
                processBtn.disabled = false;
            }
        });
    </script>
</body>
</html>

